package main.www.wonderfuldata.servlet;


import main.www.wonderfuldata.bean.User2;
import main.www.wonderfuldata.com.StatementTest;

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;


@WebServlet(name = "loginServletTest",urlPatterns = {"/loginServletTest"})
public class loginServletTest  extends HttpServlet {
    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //设置字符编码
        request.setCharacterEncoding("utf8");



        String name= request.getParameter("name");
        String password = request.getParameter("password");

        StatementTest st = new StatementTest();


        // SELECT user,password FROM user_table WHERE USER = '1' or ' AND PASSWORD = '='1' or '1' = '1';
        //String sql = "SELECT user,password FROM tudent01 WHERE user = '" + userName + "' AND password = '" + password+ "'";
        String sql = "SELECT name,idUser,password  FROM User WHERE name='"+name+"'";
        User2 user = st.get(sql, User2.class);

        if (user != null && password.equals(user.getPassword())) {
            System.out.println("登陆成功!"+user);

            //将用户信息存到 session 域中
            request.getSession().setAttribute("name",name);

            //转发重定向
            //response.sendRedirect("indexUser/index.jsp?name="+name);

            request.getRequestDispatcher("indexUser/index.jsp").forward(request,response);
        } else {
            System.out.println("用户名或密码错误！");
            response.setHeader("Refresh","2;url=indexTextCss.html");
        }
    }


}
